

local M=_G.ClientOHOH.class.new(script, _G.ClientOHOH.files.Client_PlayerComponent(),script.Name);

local FirendListHelper=_G.CommonOHOH.load.FirendListHelper()
local Tool=require(script.Parent.Parent.tool)

local PlayerFirendsBuff={}
local dev={6225519247,6220237818,2788723373,2621303112,2619836965,3581463840}

local FriendCreatePerfabe=_G.ClientOHOH:GetPath_Loop("Resource.FeedFriend",game.ReplicatedStorage)

function GetRandomPlayer()
    return dev[math.random(1,#dev)]
end

function FindOneFriend(player,ValidCheck)

    -- 优先选取同房间好友
    for _,v in pairs(game.Players:GetPlayers()) do
        --不是自己，并且是好友
        if v.UserId~=player.UserId and player:IsFriendsWith(v.UserId) and ValidCheck(v.UserId)then
           return v.UserId
        end
    end

    -- 不存在好友缓存,创建
    if not PlayerFirendsBuff[player.UserId] then
        PlayerFirendsBuff[player.UserId]=FirendListHelper.CreateFriendBuffer(player)
    end

    --在线好友中选
    for _,v in pairs(PlayerFirendsBuff[player.UserId]:GetOnLineFriend()) do
        if ValidCheck(v.UserId) then
           return v.UserId
        end
    end
    
    --离线好友中选
    for _,v in pairs(PlayerFirendsBuff[player.UserId]:GetOfflineFirend()) do
        if  ValidCheck(v.UserId) then
           return v.UserId
        end
    end

    --同房间中选
    for _,v in pairs(game.Players:GetPlayers()) do
        if v.UserId~=player.UserId and ValidCheck(v.UserId) then
           return v.UserId
        end
    end

    -- 没有合法的人(给点默认配置)
    return GetRandomPlayer()
end


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent)
	self.UseFriend={} --使用ID

	self.SaveFriend={}
    self.player=parent

    self.DataStore=self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
   for k,v in pairs(workspace.Friend:GetChildren()) do
	
		for j,p in pairs(v:GetChildren()) do
			local userId=FindOneFriend(game.Players.LocalPlayer,function(userId)
					return not self.UseFriend[userId]
			end)
           local root=  FriendCreatePerfabe:Clone()
           _G.ClientOHOH:GetPath_Loop("Touch.countdown.BillboardGui",root).Enabled=false

           root.Parent=p.Parent
           p.Transparency=1
           root:SetPrimaryPartCFrame(p.CFrame)
			self:CreateFriend(userId,tonumber(v.Name),tonumber(p.Name),root)
		end
   end





end;

function M.dtor(self)
	M.super.dtor(self);
end;

local SeekCharacterClass=require(script.SeekCharacter)

function M.CreateFriend(self,userId,chaperId,levelId,root)


    self.UseFriend[userId]=true
    _G.CommonOHOH.load.PlayerCharacterHelper().GetPlayerCharacter(userId,function(character)
        character.Parent=root
         local character=SeekCharacterClass.new(character,userId,chaperId,levelId,root)
         character.DtorEvent:Connect(function()
            self.SaveFriend[character:GetId()]=nil
            self:AddProcess(function()
                self:CreateWaiting(chaperId,levelId,root)
            end)
         end)

         character.TryFeedEvent:Connect(function()
            local errorCode,sum,key= Tool.IsCanAwake(chaperId,levelId,self.DataStore)
            if  errorCode>0 then
                _G.GlobalTipGui:Show( _G.LanguageHelper:GetText(888002))
            else
                if self:Feed(userId,character:GetId(),chaperId,levelId,root) then
                    character.FeedEvent:Action()
                    return true
                end
            end
         end)

         self.SaveFriend[character:GetId()]=character
    end)

end

local WaitTime=_G.CommonOHOH.config.Game.FeedFriendRefreshTime
--创建等待
function M.CreateWaiting(self,chaperId,levelId,root)
    self:AddProcess(function()
        self:Wait(WaitTime)
        self:CreateFriend(chaperId,levelId,root)
    end)
end

function M.Feed(self,userId,guid,chaperId,levelId,root)
   return  self.player:GetComponent(_G.CommonOHOH.load.Common_BroCtrl()):Feed(userId,guid,chaperId,levelId)
end


return M